// app/service/user.js
const Service = require('egg').Service;

class TagService extends Service {
  async mutionlyupdate(tagarr) {
    const { ctx } = this;
    let tag;
    try {
      tag = await ctx.model.Tag.bulkCreate(tagarr, {
        updateOnDuplicate: [ 'project_id', 'tag_name', 'mission_id', 'color' ],
      });
    } catch (error) {
      tag = error;
    }
    return tag;
  }

  async mutiupdate(tagarr) {
    const { ctx } = this;
    let tag;
    try {
      await ctx.model.Tag.destroy({
        where: {
          project_id: Array.from(new Set(tagarr.map(it => it.project_id))), // 所有名字为 John 的用户将被删除
        },
      });
      tag = await ctx.model.Tag.bulkCreate(tagarr, {
        updateOnDuplicate: [ 'project_id', 'tag_name', 'mission_id', 'color' ],
      });
    } catch (error) {
      tag = error;
    }
    return tag;
  }

  async muticreate(tagarr) {
    const { ctx } = this;
    let tag;
    try {
      tag = await ctx.model.Tag.bulkCreate(tagarr);
    } catch (error) {
      tag = error;
    }
    return tag;
  }

  async getbyproject_id(project_id, status) {
    const { ctx } = this;
    const where =
      status === '-1'
        ? {
          project_id,
        }
        : {
          project_id,
          status,
        };
    let tag;
    try {
      tag = await ctx.model.Tag.findAll({
        where,
      });
    } catch (error) {
      tag = error;
    }
    return tag;
  }
}

module.exports = TagService;
